package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.myview.MyListViewShiYing;
import longcaisuyun.longcai.com.net.PostDaTi;
import wh.utils.StringUtils;



/**
 * Created by Administrator on 2016/6/20.
 * 答题页面
 */
public class DaTiActivity extends Activity implements View.OnClickListener {
    private LinearLayout fanhui_lin;
    private MyListViewShiYing lv_dati;
    private Button bt_next;
    private List<PostDaTi.DaTi> list=new ArrayList<>();
    private List<TiJiao> list_tijiao=new ArrayList<>();
    private DaTiAdapter adapter;
    private boolean tiJiao=false;
    private FinalHttp finalHttp;
    private String driverid;
    private Handler mHandler=new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            int position= (int) msg.obj;
            switch (msg.what){
                case 1:
                    list.get(position).setChecked("1");
                    Log.e("dispatchMessage", list.toString());
                    adapter.notifyDataSetChanged(list);
                    break;
                case 2:
                    list.get(position).setChecked("2");
                    Log.e("dispatchMessage", list.toString());
                    adapter.notifyDataSetChanged(list);
                    break;
                case 3:
                    list.get(position).setChecked("3");
                    Log.e("dispatchMessage", list.toString());
                    adapter.notifyDataSetChanged(list);
                    break;
                case 4:
                    list.get(position).setChecked("4");
                    Log.e("dispatchMessage", list.toString());
                    adapter.notifyDataSetChanged(list);
                    break;
            }
            super.dispatchMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dati);
        MyApplication.getInstance().addActivity(this);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) getWindow().getDecorView());
        Intent intent=getIntent();
        driverid=intent.getStringExtra("driverid");
        initView();
    }

    private void initView() {
        fanhui_lin= (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(this);
        lv_dati= (MyListViewShiYing) findViewById(R.id.lv_dati);
        bt_next= (Button) findViewById(R.id.bt_next);
        bt_next.setOnClickListener(this);
        HttpPost();

    }

    private void HttpPost() {
        MyApplication.myviewutil.startProgressDialog(DaTiActivity.this);
        new PostDaTi(new AsyCallBack<PostDaTi.Info>(){
            @Override
            public void onSuccess(String toast, int type, PostDaTi.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                MyApplication.myviewutil.stopProgressDialog();
                if (info.message.equals("1")){
                    list=info.list;
                    adapter=new DaTiAdapter(DaTiActivity.this,list,mHandler);
                    lv_dati.setAdapter(adapter);
                }else{
                    UtilToast.show(DaTiActivity.this,"获取题目列表失败");
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                MyApplication.myviewutil.stopProgressDialog();
                UtilToast.show(DaTiActivity.this,"您的网络不给力");
            }
        }).execute(DaTiActivity.this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.fanhui_lin)
            finish();
        else if (v.getId()==R.id.bt_next){
            list_tijiao.clear();
            for (int i = 0; i < list.size(); i++) {
                if (StringUtils.isEmpty(list.get(i).getChecked())){
                   tiJiao=false;
                }else{
                    tiJiao=true;
                }
                TiJiao tiJiao=new TiJiao();
                tiJiao.setQuestionid(list.get(i).getId());
                switch (list.get(i).getChecked()){
                    case "1":
                        tiJiao.setOptiona("1");
                        break;
                    case "2":
                        tiJiao.setOptionb("1");
                        break;
                    case "3":
                        tiJiao.setOptionc("1");
                        break;
                    case "4":
                        tiJiao.setOptiond("1");
                        break;
                }
                list_tijiao.add(tiJiao);
            }
            if (tiJiao){
                Gson gson=new Gson();
                Type type=new TypeToken<List<PostDaTi.DaTi>>(){}.getType();
                String json = gson.toJson(list_tijiao, type);
                AjaxParams ajaxParams=new AjaxParams();
                ajaxParams.put("driverid", driverid);
                ajaxParams.put("asklist" , json);
                finalHttp=new FinalHttp();
                finalHttp.post("http://120.27.4.115:802/index.php/Api/Driver/questask", ajaxParams,new AjaxCallBack<String>() {
                    @Override
                    public void onSuccess(String s) {
                        super.onSuccess(s);
                        Log.e("post", s);
                        try {
                            JSONObject jsonObject=new JSONObject(s);
                            String message = jsonObject.optString("message");
                            if ("1".equals(message)){
                                Intent intent=new Intent(DaTiActivity.this,TiJiaoActivity.class);
                                startActivity(intent);
                            }else{
                                UtilToast.show(DaTiActivity.this, "提交失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
            }else{
                UtilToast.show(DaTiActivity.this, "请填写所有题目后提交");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        JiaMengPhotoActivity.next=false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK==keyCode){
            JiaMengPhotoActivity.next=false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public class TiJiao implements Serializable {
        private String questionid="";
        private String optiona="0";
        private String optionb="0";
        private String optionc="0";
        private String optiond="0";


        public String getQuestionid() {
            return questionid;
        }

        public void setQuestionid(String questionid) {
            this.questionid = questionid;
        }

        public String getOptiona() {
            return optiona;
        }

        public void setOptiona(String optiona) {
            this.optiona = optiona;
        }

        public String getOptionb() {
            return optionb;
        }

        public void setOptionb(String optionb) {
            this.optionb = optionb;
        }

        public String getOptionc() {
            return optionc;
        }

        public void setOptionc(String optionc) {
            this.optionc = optionc;
        }

        public String getOptiond() {
            return optiond;
        }

        public void setOptiond(String optiond) {
            this.optiond = optiond;
        }
    }
}
